package cn.pugle.base.thread.pool;


import java.io.IOException;
import java.util.concurrent.*;

/**
 * <p> Created by tzp on 2017/1/18.
 */
public class ExecutorServiceTest {
    public static void main(String[] args) {
        testException();
    }

    public static void testException() {
        ThreadPoolExecutor executorService = (ThreadPoolExecutor) Executors.newFixedThreadPool(1);
// executorService.execute(new Runnable() {
// 此处用submit, 里面把runnable封装为FutureTask, 它把异常吃了, 就不会出现我希望的replaceThread的现象了
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());
                if (Math.random() < 10) {
                    throw new RuntimeException();
                }
            }
        });

        executorService.execute(new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().getName());

            }
        });
        System.out.println("over");
    }

    public static void testScheduled() {
        ScheduledExecutorService executorService = Executors.newScheduledThreadPool(2);
        executorService.schedule(new Sleeper(), 10, TimeUnit.SECONDS);
        System.out.println("end");
    }

    public static void testThreadFactory() {
        ThreadFactory threadFactory = new ThreadFactory() {
            int i = 0;

            @Override
            public Thread newThread(Runnable r) {
                Thread s = new Thread(r, "mythread-" + i++);
                s.setDaemon(true);
                return null;
            }
        };
        //不用这个threadFactory的时候, 由于线程不是daemon的, 所以main方法退出后jvm不关闭.


        ExecutorService executorService = Executors.newSingleThreadExecutor(threadFactory);
        Future<?> future = executorService.submit(new Sleeper());
        try {
            future.get(1, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        } finally {
            boolean cancelResult = future.cancel(true);
            System.out.println("nani");
            System.out.println("cancelResult " + cancelResult);
        }
        System.out.println("over");
    }

    private static class Reader implements Runnable {

        @Override
        public void run() {
            System.out.println("reading");
            try {
                System.in.read();
//                throw new IOException();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static class Sleeper implements Runnable {
        @Override
        public void run() {
            System.out.println("sleeping");
            try {
                Thread.sleep(Long.MAX_VALUE);
            } catch (InterruptedException e) {
                System.out.println("reading interrupt");
                e.printStackTrace();
            }
        }
    }
}
